<?php
include("general_include.php");
include "checklogin.php";
include "top.php";
//include "left.php";
$profile = $_REQUEST['profile'];
$user_ladder_id = $_REQUEST['user_ladder_id'];
$extra_content = "";

if(isset($_POST['submit']))
{
   $Formval = $_POST;
   $game_id = $_POST['game_id'];
   $ladder_id = $_POST['ladder_id'];
   $server_id = $_POST['server_id'];
   $race_id = $_POST['race_id'];
   $experience = $_POST['experience'];
   $description = $_POST['description'];
   $peak_hours = $_POST['peak_hours'];
   $need_improvement = $_POST['need_improvement'];
   $user_game_id = $_POST['user_game_id'];
   $is_active = $_POST['is_active'] =="Y" ? "Y" : "N";
   $profile = $_POST['profile'];
   $is_coach = $_POST['is_coach'];
   $availability_type1 = trim($_POST['availability_type1']);
   $availability_type2 = trim($_POST['availability_type2']);
   $availability_city = trim($_POST['availability_city']);
   $cityid = $_POST['cityid'];
   $valid=1;
   
   ob_start();
   include_once "game_extra_content.php";
   $extra_content = ob_get_contents();
   ob_end_clean();
  
    $ladder_id = isset($_POST['ladder_id']) ? $_POST['ladder_id'] : '0';
	$racetxt_id = isset($_POST['race_id']) && is_array($_POST['race_id']) ? implode(",",$_POST['race_id']) : '';
	$servertxt_id = isset($_POST['server_id']) && is_array($_POST['server_id']) ? implode(",",$_POST['server_id']) : '';
	$regiontxt_id = isset($_POST['region_id']) && is_array($_POST['region_id']) ? implode(",",$_POST['region_id']) : '';
	$ratingtxt_id = isset($_POST['rating_id']) && is_array($_POST['rating_id']) ? implode(",",$_POST['rating_id']) : '';
			
   if(empty($_GET['user_game_id']))
   {
	   $dupchk = "SELECT * FROM ".TABLEPREFIX."_user_game where game_id='$game_id' and user_type_id='3' and user_id='".$_SESSION['user_id']."'";
	   $chkArr = $UserManagerObjAjax->GetRecords('Row',$dupchk);
	   $Numrow = count($chkArr);
	   if($Numrow > 0)
	   {
	     $ermsg ='This game is already added';
		 $valid = 0;  
	   }
	   if($valid ==1)
	   {
		
		  if($is_coach == 0 && $profile == 'Y')
		  {	
		     // Availability Type
			   if($availability_type1!='' && $availability_type2!='')
				$availability_type = $availability_type1.','.$availability_type2;
			   if($availability_type1!='' && $availability_type2=='')   
				 $availability_type = $availability_type1;
			   if($availability_type2!='' && $availability_type1=='')   
				 $availability_type = $availability_type2; 
				if($cityid!= '' && $availability_type2!='') 
				 {
				   $sqlCity = "SELECT * FROM ".TABLEPREFIX."_cities WHERE city_id='$cityid'";
				   $rowCity = $UserManagerObjAjax->GetRecords("Row",$sqlCity);
				   $availability_country = $rowCity['country_id'];
				 } 
			   $updateSQl = "UPDATE ".TABLEPREFIX."_user set availability_type='$availability_type',availability_city='$cityid',availability_country='$availability_country'
			   ,description='".addslashes($description)."' WHERE user_id='".$_SESSION['user_id']."'";
			   $UserManagerObjAjax->Execute($updateSQl);	 
		   }
		   if($profile == 'Y')
		   {
		      $Updatetype = "INSERT INTO ".TABLEPREFIX."_user_type_user_relation set user_type_id='3',user_id='".$_SESSION['user_id']."',date_added='".date('Y-m-d')."'";
	          $UserManagerObjAjax->Execute($Updatetype);
		   }  
		 
		   $Sql= "INSERT INTO ".TABLEPREFIX."_user_game set game_id='$game_id',ladder_id='$ladder_id',server_id='$servertxt_id',race_id='$racetxt_id', 
		   region_id='$regiontxt_id',rating_id='$ratingtxt_id', experience='".addslashes($experience)."', peak_hours='$peak_hours',
		   need_improvement='".addslashes($need_improvement)."',is_active='Y',user_type_id='3',user_id='".$_SESSION['user_id']."',date_added='".date("Y-m-d")."'";
		   $UserManagerObjAjax->Execute($Sql);
		   if(!mysql_error())
		   {
			 // header("location:my_game.php");
			  echo "<script>window.location.href='training_partner_game.php'</script>";
		   }
	   } 	   
   }
   else if(!empty($_GET['user_game_id']))
   {
       
	   $dupchk = "SELECT * FROM ".TABLEPREFIX."_user_game where game_id='$game_id' and user_type_id='3' and user_id='".$_SESSION['user_id']."' and user_game_id!='$user_game_id'";
	   $chkArr = $UserManagerObjAjax->GetRecords('Row',$dupchk);
	   $Numrow = count($chkArr);
	   if($Numrow > 0)
	   {
	     $ermsg ='This game is already added';
		 $valid = 0;  
	   }
	   if($valid ==1)
	   { 
		   $Sql1= "UPDATE ".TABLEPREFIX."_user_game set ladder_id='',server_id='',race_id='', region_id='', rating_id='' where user_game_id='".$_GET['user_game_id']."'";  
		   $UserManagerObjAjax->Execute($Sql);
		   $Sql= "UPDATE ".TABLEPREFIX."_user_game set game_id='$game_id',ladder_id='$ladder_id',server_id='$servertxt_id',race_id='$racetxt_id', 
		   region_id='$regiontxt_id', rating_id='$ratingtxt_id',experience='".addslashes($experience)."', peak_hours='$peak_hours',
		   need_improvement='".addslashes($need_improvement)."',is_active='Y',date_edited='".date("Y-m-d")."' where user_game_id='".$_GET['user_game_id']."'";
		   $UserManagerObjAjax->Execute($Sql);
		   if(!mysql_error())
		   {
			  echo "<script>window.location.href='training_partner_game.php'</script>";
		   }
	   }	   
   }	   
   

}

if(!empty($_GET['user_game_id']))
{
	/* Get Record For Display Starts */	

	$SelectgameSql="SELECT * FROM ".TABLEPREFIX."_user_game WHERE user_game_id='".$_GET['user_game_id']."'";
	$Formval = $UserManagerObjAjax->GetRecords("Row",$SelectgameSql);
	$Formval['experience'] = stripslashes($Formval['experience']);
	$Formval['need_improvement'] = stripslashes($Formval['need_improvement']);
    $SubmitButton="Edit";	
	
	// creating arrays //
	$raceArray = array();
	if(trim($Formval['race_id']) != "") $raceArray = explode(",",$Formval['race_id']);
	$serverArray = array();
	if(trim($Formval['server_id']) != "") $serverArray = explode(",",$Formval['server_id']);
	$regionArray = array();
	if(trim($Formval['region_id']) != "") $regionArray = explode(",",$Formval['region_id']);
	$ratingArray = array();
	if(trim($Formval['rating_id']) != "") $ratingArray = explode(",",$Formval['rating_id']);
	////
	$_POST['game_id'] = $Formval['game_id'];
	
	ob_start();
   include_once "game_extra_content.php";
   $extra_content = ob_get_contents();
   ob_end_clean();
	
}
else
{
	$SubmitButton="Add";
}
$countrySql = "SELECT country_id,country_name FROM ".TABLEPREFIX."_country ORDER BY country_name";
$CountryArr = $UserManagerObjAjax->HtmlOptionArrayCreate($countrySql);


$gameSql = "SELECT game_id,game_name FROM ".TABLEPREFIX."_game where is_active='Y' ORDER BY game_name";
$GameArr = $UserManagerObjAjax->HtmlOptionArrayCreate($gameSql);

$smarty->assign('extra_content',$extra_content);
$smarty->assign('Formval',$Formval);
$smarty->assign('is_coach',$is_coach);
$smarty->assign('is_partner',$is_partner);
$smarty->assign('profile',$profile);
$smarty->assign('ermsg',$ermsg);
$smarty->assign('CountryArr',$CountryArr);
$smarty->assign('Formval',$Formval);
$smarty->assign('SubmitButton',$SubmitButton);
$smarty->assign('GameArr',$GameArr);
$smarty->assign('user_game_id',$user_game_id);
$smarty->display("training_partner_game_update.tpl");
include("footer.php");
?>